package org.dromara.system.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.TalentDeclarationInfoBo;
import org.dromara.system.domain.vo.SysDeptVo;
import org.dromara.system.domain.vo.SysOssUploadVo;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.domain.vo.TalentDeclarationInfoVo;
import org.dromara.system.service.ISysOssService;
import org.dromara.system.service.ITalentDeclarationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 人才申报信息
 *
 * @author 毛宁川
 * @date 2025-09-05
 */
@Validated
@RestController
@RequestMapping("/system/declarationInfo")
public class TalentDeclarationInfoController extends BaseController {

    @Autowired
    private ITalentDeclarationInfoService talentDeclarationInfoService;

    @Autowired
    private ISysOssService ossService;

    /**
     * 查询人才申报信息列表
     */
    @SaCheckPermission("system:declarationInfo:list")
    @GetMapping("/list")
    public TableDataInfo<TalentDeclarationInfoVo> list(TalentDeclarationInfoBo bo, PageQuery pageQuery) {
        return talentDeclarationInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出人才申报信息列表
     */
    @SaCheckPermission("system:declarationInfo:export")
    @Log(title = "人才申报信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TalentDeclarationInfoBo bo, HttpServletResponse response) {
        List<TalentDeclarationInfoVo> list = talentDeclarationInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "人才申报信息", TalentDeclarationInfoVo.class, response);
    }

    /**
     * 获取人才申报信息详细信息
     *
     * @param infoId 主键
     */
    @SaCheckPermission("system:declarationInfo:query")
    @GetMapping("/{infoId}")
    public R<TalentDeclarationInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                              @PathVariable Long infoId) {
        return R.ok(talentDeclarationInfoService.queryById(infoId));
    }

    /**
     * 新增人才申报信息
     */
    @SaCheckPermission("system:declarationInfo:add")
    @Log(title = "人才申报信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TalentDeclarationInfoBo bo) {
        return toAjax(talentDeclarationInfoService.insertByBo(bo));
    }

    /**
     * 修改人才申报信息
     */
    @SaCheckPermission("system:declarationInfo:edit")
    @Log(title = "人才申报信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TalentDeclarationInfoBo bo) {
        return toAjax(talentDeclarationInfoService.updateByBo(bo));
    }

    /**
     * 删除人才申报信息
     *
     * @param infoIds 主键串
     */
    @SaCheckPermission("system:declarationInfo:remove")
    @Log(title = "人才申报信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] infoIds) {
        return toAjax(talentDeclarationInfoService.deleteWithValidByIds(List.of(infoIds), true));
    }


    /**
     * 查询申报信息列表
     */
    @GetMapping("/getDeclarationInfoList")
    public R<List<TalentDeclarationInfoVo>> getDeclaration() {
        Long userId = LoginHelper.getLoginUser().getUserId();
        List<TalentDeclarationInfoVo> talentDeclarationInfoVo = talentDeclarationInfoService.getDeclarationList(userId);
        return R.ok(talentDeclarationInfoVo);
    }

    @GetMapping("/getDeclarationInfo/{infoId}")
    public R<TalentDeclarationInfoVo> getDeclaration(@PathVariable Long infoId) {
        Long userId = LoginHelper.getLoginUser().getUserId();
        TalentDeclarationInfoVo talentDeclarationInfoVo = talentDeclarationInfoService.getDeclarationByInfoIdAndUserId(infoId, userId);
        return R.ok(talentDeclarationInfoVo);
    }


    /**
     * 小程序用户提交申报
     */
    @PostMapping("/submitDeclarationInfo")
    public R<Void> submitDeclarationInfo(@RequestBody TalentDeclarationInfoBo talentDeclarationInfoBo) {
        Long userId = LoginHelper.getUserId();
        return toAjax(talentDeclarationInfoService.submitDeclarationInfo(talentDeclarationInfoBo, userId));
    }

    /**
     * 小程序用户更新申报
     */
    @Log(title = "人才申报信息更新", businessType = BusinessType.UPDATE)
    @PostMapping("/submitDeclarationInfoUpdate")
    public R<Void> update(@RequestBody TalentDeclarationInfoBo bo) {
        return toAjax(talentDeclarationInfoService.submitDeclarationInfoUpdate(bo));
    }


    @Log(title = "上传证明材料接口", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysOssUploadVo> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            return R.fail("上传文件不能为空");
        }
        SysOssVo oss = ossService.upload(file);
        SysOssUploadVo uploadVo = new SysOssUploadVo();
        uploadVo.setUrl(oss.getUrl());
        uploadVo.setFileName(oss.getOriginalName());
        uploadVo.setOssId(oss.getOssId().toString());
        return R.ok(uploadVo);
    }

    @Log(title = "分配部门")
    @SaCheckPermission("system:declarationInfo:allot")
    @PostMapping(value = "/assignDept")
    public R<Void> assignDept(@RequestBody TalentDeclarationInfoBo bo) {
        return toAjax(talentDeclarationInfoService.assignDept(bo));
    }

    @Log(title = "退回部门")
    @SaCheckPermission("system:declarationInfo:return")
    @PostMapping(value = "/returnDept")
    public R<Void> returnDept(@RequestBody TalentDeclarationInfoBo bo) {
        return toAjax(talentDeclarationInfoService.returnDept(bo));
    }


    @Log(title = "查询部门所有可用部门列表")
    @GetMapping(value = "/optionDepts")
    public R<List<SysDeptVo>> optionDepts() {
        return R.ok(talentDeclarationInfoService.optionDepts());
    }
}
